package com.xudow.app.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.user.UserWithUserProfileAndRelation;
import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.adapter.AccountRelatedAdapter;
import com.xudow.app.ui.task.StudentListTask;
import com.xudow.app.ui.task.user.RelatedAccountTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 关联账号Activity
 *
 * @author lukecheng
 */
//TODO:关联用户是否可以删除
public class AccountRelatedActivity extends BaseActivity {

    private RelatedAccountTask relatedAcoountTask;

    private ListView listView;
    private ProgressBar progressBar;

    private AccountRelatedAdapter listAdapter;
    private List<UserWithUserProfileAndRelation> users;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_related);

        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        listView = (ListView) findViewById(R.id.list);
    }

    @Override
    protected void onResume() {
        super.onResume();

        relatedAcoountTask = new RelatedAccountTask(this, relatedAccountTaskHandler);
        addTask(relatedAcoountTask);
        relatedAcoountTask.execute();
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler relatedAccountTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (RelatedAccountTask.SUCCESS == msg.what) {
                UserWithUserProfileAndRelation[] dataArr = (UserWithUserProfileAndRelation[]) msg.getData()
                        .getSerializable("users");

                if (null == dataArr || dataArr.length <= 0) {
                    progressBar.setVisibility(View.GONE);
                    return;
                }

                users = new ArrayList<UserWithUserProfileAndRelation>();
                users.addAll(Arrays.asList(dataArr));

                listAdapter = new AccountRelatedAdapter(AccountRelatedActivity.this,
                        R.layout.item_account_relative, users);

                progressBar.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);

                listView.setAdapter(listAdapter);
            } else {
                progressBar.setVisibility(View.GONE);

                String message = getString(R.string.loading_fail);
                Toast.makeText(AccountRelatedActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        }
    };

    /**
     * 添加关联点击
     *
     * @param view
     */
    public void onAddClick(View view) {
        Intent intent = new Intent(this, AccountRelateAddActivity.class);
        startActivity(intent);
    }
}
